package cn.edu.fzu.homemaking.company.controller;

import cn.edu.fzu.homemaking.company.api.OrderApi;
import cn.edu.fzu.homemaking.company.config.CompanyConfig;
import cn.edu.fzu.homemaking.company.domain.entity.Order;
import cn.edu.fzu.homemaking.company.domain.enums.CommentStatusEnum;
import cn.edu.fzu.homemaking.company.domain.enums.OrderStatusEnum;
import cn.edu.fzu.homemaking.util.ResultUtil;
import cn.edu.fzu.homemaking.web.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author wanjie
 * @version 1.0
 */
@RestController
@RequestMapping("/api/company/auth/order")
@Api(tags = "订单管理相关接口")
public class OrderController {

    @Resource
    private OrderApi       orderApi;

    @Resource
    private RabbitTemplate rabbitTemplate;

    //展示所有的订单
    @GetMapping("/list")
    @ApiOperation(value = "获取公司订单列表", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "demandId", value = "需求id"),
            @ApiImplicitParam(name = "serverId", value = "服务id"),
            @ApiImplicitParam(name = "startTime", value = "开始服务日期"),
            @ApiImplicitParam(name = "endTime", value = "结束服务日期"),
            @ApiImplicitParam(name = "status", value = "状态，REFUND=已退款，CANCEL=已取消，ORDER=已下单，DISPATCH=已分派人员，GOING=进行中，PAYING=待支付，ENDED=已结束"),
            @ApiImplicitParam(name = "commentStatus", value = "订单评价状态: 0=待评价，1=已评价"),
            @ApiImplicitParam(name = "pageNum", required = true),
            @ApiImplicitParam(name = "pageSize", required = true) })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result listOrder(Long demandId, Long serverId, String startTime, String endTime, OrderStatusEnum status,
                            CommentStatusEnum commentStatus, Integer pageNum, Integer pageSize) {
        return orderApi.list(demandId, serverId, startTime, endTime, status, commentStatus, pageNum, pageSize);
    }


    //查询单个订单
    @GetMapping("/info")
    @ApiOperation(value = "单个订单详情", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "orderId", value = "订单id", required = true) })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result singleOrder(String orderId) {
        return orderApi.single(orderId);
    }


    @GetMapping("/cancel")
    @ApiOperation(value = "取消订单", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "orderId", value = "订单id", required = true) })
    public Result cancel(String orderId) {
        Order order = ResultUtil.parseValue(orderApi.single(orderId), Order.class);
        Assert.notNull(order, "订单不存在");
        Assert.isTrue(order.getStatus() == OrderStatusEnum.ORDER, "只能取消已下单的订单");
        return orderApi.updateOrderStatus(orderId, OrderStatusEnum.CANCEL);
    }


    @GetMapping("/dispatch")
    @ApiOperation(value = "分派订单", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "orderId", value = "订单id", required = true) })
    public Result dispatch(String orderId) {
        Order order = ResultUtil.parseValue(orderApi.single(orderId), Order.class);
        Assert.notNull(order, "订单不存在");
        Assert.isTrue(order.getStatus() == OrderStatusEnum.ORDER, "仅已下单的订单才能被分派");
        orderApi.updateOrderStatus(orderId, OrderStatusEnum.DISPATCH);
        rabbitTemplate.convertAndSend(CompanyConfig.EXCHANGE_TOPICS_ORDER, "order.deal", order);
        return Result.SUCCESS;
    }


    //订单统计概览
    @GetMapping("count")
    @ApiOperation(value = "订单统计概览", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "startTime", value = "开始时间（订单创建时间）"),
            @ApiImplicitParam(name = "endTime", value = "结束时间（订单创建时间）") })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result count(String startTime, String endTime) {
        return orderApi.count(startTime, endTime);
    }


    //按月统计订单
    @GetMapping("countByMonth")
    @ApiOperation(value = "按月统计订单", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "startTime", value = "开始时间（订单创建时间）", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间（订单创建时间）", required = true) })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result countByMonth(String startTime, String endTime) {
        return orderApi.countByMonth(startTime, endTime);
    }


    //按天统计订单
    @GetMapping("countByDay")
    @ApiOperation(value = "按天统计订单", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "startTime", value = "开始时间（订单创建时间）", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间（订单创建时间）", required = true) })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result countByDay(String startTime, String endTime) {
        return orderApi.countByDay(startTime, endTime);
    }
}
